package com.lanou.beautifulwardrobe.activity_zhuti;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity_zhuti.adapter.ActivityViewPagerAdapter;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.WelfareData;
import com.lanou.beautifulwardrobe.entify.WelfareResult;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class ActivityFragment extends Fragment {

    private static ActivityFragment activityFragment;
    private TabLayout activityTabLayout;
    private ViewPager viewPager;
    private ActivityViewPagerAdapter adapter;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();

    public static ActivityFragment getInstance() {
        if (activityFragment == null) {
            synchronized (ActivityFragment.class) {
                if (activityFragment == null)
                    activityFragment = new ActivityFragment();
            }
        }
        return activityFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mainfragment_activity, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        activityTabLayout = (TabLayout) view.findViewById(R.id.activityTabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.activityViewPager);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        adapter = new ActivityViewPagerAdapter(getFragmentManager());
    }

    private void initData() {
        activityTabLayout.setTabMode(TabLayout.MODE_FIXED);

        volleySingleton.getStringRequset(Contants.ACTIVITY_ITEM_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                WelfareResult welfareResult = gson.fromJson(response, WelfareResult.class);
                if (welfareResult.getMsg().equals("ok")) {
                    List<WelfareData> welfareDatas = welfareResult.getData();
                    adapter.addData(welfareDatas);
                    viewPager.setAdapter(adapter);
                    activityTabLayout.setupWithViewPager(viewPager);
                }
            }
        });


    }
}
